var _ajax = require('../../utils/ajax');
var _Server = require('../../utils/server');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        switch: 1,
        page: 1,
        type: 2,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.fnAjax();
    },
    fnAjax: function (options) {
        var that = this;
        _ajax.myProductList(that.data.switch, that.data.type, that.data.page, function (data) {
            console.log(data);
            if (data.code == 0) {
                that.setData({
                    indexUrl: _Server.url,
                    myProductListData: data.data,
                });
                if (data.data.length == 0) {
                    wx.showToast({
                        title: '暂无数据',
                    });
                }
            }
        }, this);
    },
    // 切换函数
    fnSwitchBlock: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        that.setData({
            switch: index
        });
        that.fnAjax();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        _ajax.myProductList(that.data.switch, that.data.page + 1, function (data) {
            if (data.code == 0) {
                if (data.data.length <= 0) {
                    wx.showToast({
                        title: '没有数据了',
                        icon: 'success',
                        duration: 2000
                    });
                } else {
                    var datas = that.data.myProductListData.concat(data.data);
                    that.setData({
                        myProductListData: datas,
                        page: that.data.page + 1
                    });
                }
            }
        }, this);
    },
})